import type { ICityQuery, IRegionQuery } from '@/api/popup';
import type { IResponse } from '../types/index';
import type { ISelectItem } from '@/types/popup';

const data = [
  {
    name: '河南',
    value: 1,
    city: [
      {
        name: '郑州',
        value: 101,
        region: [
          {
            name: '经开区',
            value: 10101,
          },
          {
            name: '管城区',
            value: 10102,
          },
        ],
      },
      {
        name: '焦作',
        value: 102,
        region: [
          {
            name: '高新区',
            value: 10201,
          },
          {
            name: '山阳区',
            value: 10202,
          },
        ],
      },
    ],
  },
  {
    name: '河北',
    value: 2,
    city: [
      {
        name: '石家庄',
        value: 201,
        region: [
          {
            name: '区域1',
            value: 20101,
          },
          {
            name: '区域2',
            value: 20102,
          },
        ],
      },
      {
        name: '邯郸',
        value: 202,
        region: [
          {
            name: '区域12',
            value: 20201,
          },
          {
            name: '区域13',
            value: 20202,
          },
        ],
      },
    ],
  },
];

export default [
  {
    url: '/mock/provice',
    method: 'get',
    response: (): IResponse<ISelectItem[]> => {
      return {
        code: 200,
        msg: 'ok',
        data: data.map((item) => {
          return { name: item.name, value: item.value };
        }),
      };
    },
  },
  {
    url: '/mock/city',
    method: 'get',
    response: ({ query }: { query: ICityQuery }): IResponse<ISelectItem[]> => {
      const city = data
        ?.find((item) => item.value == query.pid)
        ?.city.map((item) => {
          return { name: item.name, value: item.value };
        });
      if (city) {
        return {
          code: 200,
          msg: 'ok',
          data: city,
        };
      } else {
        return {
          code: 200,
          msg: '未找到数据',
          data: [],
        };
      }
    },
  },
  {
    url: '/mock/region',
    method: 'get',
    response: ({ query }: { query: IRegionQuery }): IResponse<ISelectItem[]> => {
      const region = data
        ?.find((item) => item.value == query.pid)
        ?.city.find((item) => item.value == query.cid)
        ?.region.map((item) => {
          return { name: item.name, value: item.value };
        });
      if (region) {
        return {
          code: 200,
          msg: 'ok',
          data: region,
        };
      } else {
        return {
          code: 200,
          msg: '未找到数据',
          data: [],
        };
      }
    },
  },
];
